<template>
  <el-dialog
    :title="!dataForm.saleId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="楼盘名字" prop="saleName">
      <el-input v-model="dataForm.saleName" placeholder="楼盘名字"></el-input>
    </el-form-item>
    <el-form-item label="楼盘地址" prop="saleAddress">
      <el-input v-model="dataForm.saleAddress" placeholder="楼盘地址"></el-input>
    </el-form-item>
    <el-form-item label="楼盘地址" prop="salePrice">
      <el-input v-model="dataForm.salePrice" placeholder="楼盘地址"></el-input>
    </el-form-item>
    <el-form-item label="时间" prop="saleTime">
      <el-input v-model="dataForm.saleTime" placeholder="时间"></el-input>
    </el-form-item>
    <el-form-item label="经纪人" prop="agentId">
      <el-input v-model="dataForm.agentId" placeholder="经纪人"></el-input>
    </el-form-item>
    <el-form-item label="是否开盘：0开盘 1未开盘" prop="saleState">
      <el-input v-model="dataForm.saleState" placeholder="是否开盘：0开盘 1未开盘"></el-input>
    </el-form-item>
    <el-form-item label="删除：0删除1未删除" prop="saleDel">
      <el-input v-model="dataForm.saleDel" placeholder="删除：0删除1未删除"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          saleId: 0,
          saleName: '',
          saleAddress: '',
          salePrice: '',
          saleTime: '',
          agentId: '',
          saleState: '',
          saleDel: ''
        },
        dataRule: {
          saleName: [
            { required: true, message: '楼盘名字不能为空', trigger: 'blur' }
          ],
          saleAddress: [
            { required: true, message: '楼盘地址不能为空', trigger: 'blur' }
          ],
          salePrice: [
            { required: true, message: '楼盘地址不能为空', trigger: 'blur' }
          ],
          saleTime: [
            { required: true, message: '时间不能为空', trigger: 'blur' }
          ],
          agentId: [
            { required: true, message: '经纪人不能为空', trigger: 'blur' }
          ],
          saleState: [
            { required: true, message: '是否开盘：0开盘 1未开盘不能为空', trigger: 'blur' }
          ],
          saleDel: [
            { required: true, message: '删除：0删除1未删除不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.saleId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.saleId) {
            this.$http({
              url: this.$http.adornUrl(`/house/sale/info/${this.dataForm.saleId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.saleName = data.sale.saleName
                this.dataForm.saleAddress = data.sale.saleAddress
                this.dataForm.salePrice = data.sale.salePrice
                this.dataForm.saleTime = data.sale.saleTime
                this.dataForm.agentId = data.sale.agentId
                this.dataForm.saleState = data.sale.saleState
                this.dataForm.saleDel = data.sale.saleDel
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/sale/${!this.dataForm.saleId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'saleId': this.dataForm.saleId || undefined,
                'saleName': this.dataForm.saleName,
                'saleAddress': this.dataForm.saleAddress,
                'salePrice': this.dataForm.salePrice,
                'saleTime': this.dataForm.saleTime,
                'agentId': this.dataForm.agentId,
                'saleState': this.dataForm.saleState,
                'saleDel': this.dataForm.saleDel
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
